package com.spring.Controller;

import com.spring.Controller.Interface.AdminController;
import com.spring.Entity.AdminEntity;
import com.spring.Service.Interface.AdminService;
import com.spring.Util.MD5Util;
import com.spring.Util.RSAUtils;
import com.spring.Util.STATUS;
import jxl.read.biff.BiffException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.IncorrectCredentialsException;
//import org.apache.shiro.authc.UnknownAccountException;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.subject.Subject;
//import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by CMCC-sunmo on 2016/8/26.
 */
@Controller
@RequestMapping("/admin")
public class AdminControllerImpl implements AdminController {

    @Autowired
    private AdminService adminService;

    public Map prepare(Map map) {
        RSAPublicKey publicKey = RSAUtils.getDefaultPublicKey();
        map.put("modulus", new String(Hex.encodeHex(publicKey.getModulus().toByteArray())));
        map.put("exponent", new String(Hex.encodeHex(publicKey.getPublicExponent().toByteArray())));
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public Map logIn() {
        Map<String, Object> response = new HashMap<>();
        response = prepare(response);
        return response;
    }

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Map testLogin(AdminEntity adminEntity, HttpSession session, HttpServletRequest request) {
        Map response = new HashMap();
        AdminEntity check = adminService.findByAdminName(adminEntity.getAdminName());
        System.out.println("key:" + MD5Util.calc(adminEntity.getAdminPassword()));
        String des = adminService.login(adminEntity.getAdminName(), adminEntity.getAdminPassword());
        if (StringUtils.isBlank(des)) {
            session.setAttribute("SID", check.getAdminID());
            session.setAttribute("SName", check.getAdminName());
            System.out.println(session.getAttribute("SID"));
            response.put("status", "ok");
            response.put("description", "登录成功");
        } else {
            response.put("status", "error");
            response.put("description", des);
        }
        return response;
    }

    @ResponseBody
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public Map<String, Object> logOut(HttpSession session, HttpServletRequest httpServletRequest) {
        Map<String, Object> response = new HashMap<String, Object>();
        System.out.println(String.valueOf(session.getAttribute("SID")));
        if (!String.valueOf(session.getAttribute("SID")).equals("null")) {
            session.removeAttribute("SID");
            response.put("status", "ok");
            response.put("description", "注销成功");
        } else {
            response.put("status", "error");
            response.put("description", "已登出");
        }
        session.invalidate();
        return response;
    }

    //手动添加用户
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Override
    public Map<String, Object> addAdmin(AdminEntity adminEntity, HttpServletRequest request) {
        Map<String, Object> response = new HashMap<String, Object>();
        if (StringUtils.isBlank(adminEntity.getAdminID())
                || StringUtils.isBlank(adminEntity.getAdminName())
                || StringUtils.isBlank(adminEntity.getAdminPassword())) {
            response.put("status", "empty");
            response.put("description", "id或用户名或密码或职位为空");
            return response;
        }
        if (!adminService.checkAdmin(adminEntity)) {
            response.put("status", "exists");
            response.put("description", "用户已存在");
            return response;
        }
        int status = adminService.addAdmin(adminEntity);
        if (status == STATUS.SUCCESS) {
            response.put("status", "ok");
            response.put("description", "添加成功");
        }
        if (status == STATUS.FAIL) {
            response.put("status", "fail");
            response.put("description", "添加失败");
        }
        return response;
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Map<String, Object> updateAdmin(HttpSession session, String adminName, HttpServletRequest request) {
        Map<String, Object> response = new HashMap<String, Object>();
        AdminEntity adminEntity;
        String adminId = (String) session.getAttribute("SID");
        if (adminId.equals("null")) {
            response.put("status", "refuse");
            response.put("description", "未登录");
            return response;
        }
        adminEntity = adminService.findById(adminId);
        if (adminService.checkAdmin(adminEntity)) {
            response.put("status", "empty");
            response.put("description", "用户不存在");
            return response;
        }
        int status = adminService.updateAdmin(adminEntity, adminName);
        if (status == STATUS.SUCCESS) {
            response.put("status", "ok");
            response.put("description", "修改成功");
        } else if (status == STATUS.FORMAT_ERROR) {
            response.put("status", "error");
            response.put("description", "填写信息格式错误");
        }
        return response;
    }

    //修改密码
    @ResponseBody
    @RequestMapping(value = "/change", method = RequestMethod.POST)
    public Map<String, Object> changePassword(HttpSession session, String oldPassword, String newPassword, HttpServletRequest request) {
        Map<String, Object> response = new HashMap<>();
        String adminId = (String) session.getAttribute("SID");
        if (StringUtils.isBlank(adminId)) {
            response.put("status", "refuse");
            response.put("description", "未登录");
            return response;
        }
        AdminEntity check = new AdminEntity();
        check.setAdminID(adminId);
        if (adminService.checkAdmin(check)) {
            response.put("status", "empty");
            response.put("description", "用户不存在");
            return response;
        }
        int status = adminService.changePassword(adminId, oldPassword, newPassword);
        if (status == STATUS.SUCCESS) {
            response.put("status", "ok");
            response.put("description", "修改密码成功");
        } else {
            response.put("status", "fail");
            response.put("description", "修改失败");
        }
        return response;
    }

    //通过ID查找用户信息
    @ResponseBody
    @RequestMapping(value = "/byAdminId", method = RequestMethod.POST)
    public Map<String, Object> findById(HttpSession session, HttpServletRequest request) {
        AdminEntity admin = null;
        Map<String, Object> response = new HashMap<String, Object>();
        String adminId = (String) session.getAttribute("SID");
        if (StringUtils.isBlank(adminId)) {
            response.put("status", "refuse");
            response.put("description", "未登录");
            return response;
        }
        admin = adminService.findById(adminId);
        if (null != admin) {
            admin.setAdminPassword("********");
            response.put("status", "ok");
            response.put("body", admin);
        } else {
            response.put("status", "error");
            response.put("description", "用户不存在");
        }
        return response;
    }

    @ResponseBody
    @RequestMapping(value = "/byAdminName", method = RequestMethod.POST)
    public Map<String, Object> findByAdminName(String adminName, HttpSession session, HttpServletRequest request) {
        AdminEntity admin = null;
        Map<String, Object> response = new HashMap<String, Object>();
        admin = adminService.findByAdminName(adminName);
        if (null != admin) {
            admin.setAdminPassword("********");
            response.put("status", "ok");
            response.put("body", admin);
        } else {
            response.put("status", "error");
            response.put("description", "用户不存在");
        }
        return response;
    }
}
